Given a_m_x_n_grid filled with non-negative numbers, find a path from top left to bottom right which_minimizes_the sum of all numbers along its path.
Note:You can only move either down or right at any point in time.
Example:
Input:
[
[1,3,1],
[1,5,1],
[4,2,1]
]
Output:
7
Explanation:
Because the path 1→3→1→1→1 minimizes the sum.
class Solution {
public:
int minPathSum\(vector<vector<int>>& grid\) {
int m=grid.size\(\);
int n=grid\[0\].size\(\);
int cost\[m+1\]\[n+1\];
cost\[1\]\[1\]=grid\[0\]\[0\];
for \(int i=2;i<=m;i++\){
cost\[i\]\[1\]=cost\[i-1\]\[1\]+grid\[i-1\]\[0\];
}
for \(int j=2;j<=n;j++\){
cost\[1\]\[j\]=cost\[1\]\[j-1\]+grid\[0\]\[j-1\];
}
for \(int i=2;i<=m;i++\){
for\(int j=2;j<=n;j++\){
cost\[i\]\[j\]=grid\[i-1\]\[j-1\]+min\(cost\[i-1\]\[j\],cost\[i\]\[j-1\]\);
//cout<<cost\[i\]\[j\]<<endl;
}
}
return cost\[m\]\[n\];
}
};